package com.example.swordjava;


import java.util.ArrayList;
import java.util.List;

public class Test29 {

    public static void main(String[] args) {

        int[][] a = {{1, 2, 3, 4},
                {5, 6, 7, 8},
                {9, 10, 11, 12},
                {13, 14, 15, 16}};

        System.out.println(a[0][1]);

    }

    class TreeNode {
        TreeNode left = null;
        TreeNode right = null;
        int val;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    /**
     * 顺时针打印矩阵
     * 输入一个矩阵，按照从外向里以顺时针的顺序依次打印出每一个数字，例如，如果输入如下4 X 4矩阵：
     * [[1,2,3,4],
     * [5,6,7,8],
     * [9,10,11,12],
     * [13,14,15,16]]
     */

    public ArrayList<Integer> printMatrix(int[][] matrix) {
        ArrayList<Integer> ans = new ArrayList<>();
        if (matrix.length == 0 || matrix[0].length == 0) {
            return ans;
        }

        int left = 0;
        int right = matrix[0].length - 1;
        int top = 0;
        int bottom = matrix.length - 1;

        while (left <= right && top <= bottom) {
            for (int i = left; i <= right; i++) ans.add(matrix[top][i]);
            for (int i = top + 1; i <= bottom; i++) ans.add(matrix[i][right]);
            for (int i = right - 1; i >= left && top < bottom; i--) ans.add(matrix[bottom][i]);
            for (int i = bottom - 1; i > top && right > left; i--) ans.add(matrix[i][left]);
            left++;
            right--;
            top++;
            bottom--;
        }
        return ans;

    }

}
